package com.vote.www.service;

import com.vote.www.core.page.Pagination;
import com.vote.www.domain.Czjuserapprove;
import com.vote.www.domain.Firsttrip;
import com.vote.www.persistence.ApproveMapper;
import com.vote.www.persistence.FirsttripMapper;
import com.vote.www.persistence.SpokesmanMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 汤伟杰 on 2017/5/23.
 */
@Service
public class FirsttripService {
    @Autowired
    private FirsttripMapper firsttripMapper;
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private SpokesmanMapper spokesmanMapper;
    @Autowired
    private CzjuserapproveService czjuserapproveService;

    public Pagination list(int pageNo, int pageSize, Map<String, Object> params, ModelMap model) throws Exception {

        Pagination pagination = new Pagination();
        pagination.setPageNo(pageNo);
        pagination.setPageSize(pageSize);
        params.putAll(pagination.toMap());
        int count = firsttripMapper.getTotalRows(params);
        List<Map<String, Object>> list = firsttripMapper.getList(params);
        pagination.setTotalCount(count);
        pagination.setList(list);
        return pagination;
    }


    public void insert(Firsttrip firsttrip){
        firsttripMapper.insert(firsttrip);
    }

    public void update(Firsttrip firsttrip){
        firsttripMapper.update(firsttrip);
    }
    public void deleteById(String firsttripId){
        firsttripMapper.deleteById(firsttripId);
    }
    public void deleteByIds(List<String> list){
        firsttripMapper.deleteByIds(list);
    }
    public Firsttrip queryById(String id){
        return firsttripMapper.queryById(id);
    }
    public List<Firsttrip> queryByIds(List<String> list){
        List<Firsttrip> listFirsttrip = firsttripMapper.queryByIds(list);
        if(listFirsttrip==null){
            listFirsttrip = new ArrayList<Firsttrip>();
        }
        return listFirsttrip;
    }

    public List<Map<String,Object>> getUserApproveList() {
        Map<String, Object> params = new HashMap<String, Object>();
        List<Map<String, Object>> listbean = spokesmanMapper.getByType(params);
        List<Map<String, Object>> listmap = new ArrayList<Map<String, Object>>();
        Map<String,Object> mapobj = null;
        for (Map<String, Object> map : listbean) {
            mapobj = new HashMap<String, Object>();
            mapobj.put("userType",map.get("represent"));
            mapobj.put("userIcon",map.get("picPath"));
            mapobj.put("userNum",map.get("count"));
            mapobj.put("userId",map.get("userinfoId"));
            mapobj.put("locus",map.get("locus"));
            mapobj.put("nick",map.get("nick"));
            listmap.add(mapobj);
        }
        return listmap;
    }

    public void queryDetailById(String firsttripId, ModelMap model) {
        Firsttrip f = queryById(firsttripId);

        if(StringUtils.isNotBlank(f.getUserapproveId())){
            Czjuserapprove czjuserapprove = (Czjuserapprove) jdbcTemplate.queryForObject("select * from czj_userapprove where userinfoId='" + f.getUserapproveId() + "' and isuse='1' limit 1", new BeanPropertyRowMapper(Czjuserapprove.class));
            try {
                czjuserapprove.setAge(czjuserapproveService.idcardToAge(czjuserapprove.getIdCard()));
            } catch (Exception e) {
                e.printStackTrace();
            }
            model.addAttribute("userapprove", czjuserapprove);
        }else{
            model.addAttribute("userapprove", new Czjuserapprove());
        }

        model.addAttribute("entity", f);



    }
}
